﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DreamBox.Admin.Posts
{
    public partial class ClassCompetence : BLL.BasePage
    {
        DAL.PSelect ps = new DAL.PSelect();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
               

                drop_PostsClass.DataSource = ps.Select("dbox_PostsClass","CID,ClassName","");
                drop_PostsClass.DataTextField = "ClassName";
                drop_PostsClass.DataValueField = "CID";
                drop_PostsClass.DataBind();

                if (Request.QueryString["CID"] != null)
                {
                    foreach (ListItem item in drop_PostsClass.Items)
                    {
                        if (item.Value == Request.QueryString["CID"])
                        {
                            item.Selected = true;
                        }
                    }
                }
                Bind();
            }
        }
        void Bind()
        {
            DataTable dt = ps.Select("dbox_UserGroup", "GID,GroupName", "");
            RepeaterList.DataSource = dt;
            RepeaterList.DataBind();
            //处理数据加载
            DataTable dt1 = ps.Select("dbox_PostsClassCompetence", "*", "CID=" + drop_PostsClass.SelectedValue);

            HiddenField hide_ID = null;

            HiddenField hide_GID = null;
            CheckBox cbox_IsPosts = null;
            CheckBox cbox_IsReply = null;
            CheckBox cbox_IsRead = null;
            foreach (RepeaterItem item in RepeaterList.Items)
            {
                hide_ID = item.FindControl("hide_ID") as HiddenField;
                hide_GID = item.FindControl("hide_GID") as HiddenField;
                cbox_IsPosts = item.FindControl("cbox_IsPosts") as CheckBox;
                cbox_IsReply = item.FindControl("cbox_IsReply") as CheckBox;
                cbox_IsRead = item.FindControl("cbox_IsRead") as CheckBox;

                foreach (DataRow dr in dt1.Rows)
                {
                    if (dr["GID"].ToString() == hide_GID.Value)
                    {
                        hide_ID.Value = dr["ID"].ToString();

                        cbox_IsPosts.Checked = bool.Parse(dr["IsPosts"].ToString());
                        cbox_IsReply.Checked = bool.Parse(dr["IsReply"].ToString());
                        cbox_IsRead.Checked = bool.Parse(dr["IsRead"].ToString());
                    }
                }
            }
        }
        protected void btn_Post_Click(object sender, EventArgs e)
        {
            DAL.MAction action = new DAL.MAction();
            Model.PostsClassCompetence m = new Model.PostsClassCompetence();
            //处理数据
            HiddenField hide_ID = null;

            HiddenField hide_GID = null;
            CheckBox cbox_IsPosts = null;
            CheckBox cbox_IsReply = null;
            CheckBox cbox_IsRead = null;
            foreach (RepeaterItem item in RepeaterList.Items)
            {
                hide_ID = item.FindControl("hide_ID")  as HiddenField;
                hide_GID = item.FindControl("hide_GID") as HiddenField;
                cbox_IsPosts = item.FindControl("cbox_IsPosts") as CheckBox;
                cbox_IsReply = item.FindControl("cbox_IsReply") as CheckBox;
                cbox_IsRead = item.FindControl("cbox_IsRead") as CheckBox;

                m.IsPosts = cbox_IsPosts.Checked;
                m.IsReply = cbox_IsReply.Checked;
                m.IsRead = cbox_IsRead.Checked;

                if (hide_ID.Value != "")
                {
                    m.ID = int.Parse(hide_ID.Value);
                    action.Update(m);
                }
                else
                {
                    m.GID = int.Parse(hide_GID.Value);
                    m.CID = int.Parse(drop_PostsClass.SelectedValue);
                    action.Insert(m);
                }
            }
            BLL.Admin.Tips.DetailedTips(this, "操作成功", "/Posts/ClassCompetence.aspx?CID="+drop_PostsClass.SelectedValue, "1");
        }

        protected void drop_PostsClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }
    }
}